/*
 * Copyright (C) 拾光分享网 
 * All rights reserved, Designed By www.hiai.top.
 * Email：goodsking@163.com.
 * Version：V1.0.2(20230701).
 */
package top.hiai._admin.user;

import com.jfinal.core.Controller;
import com.jfinal.validate.Validator;

import top.hiai.common.util.SensitiveWordsKit;

/**
* @Author www.hiai.top
* @Email  goodsking@163.com
* @Message 更多资源尽在www.hiai.top
* @Introduction 类的介绍
*/
public class UserAdminValidator extends Validator {

	@Override
	protected void validate(Controller c) {
		setShortCircuit(true);

		/**
		 * 验证 nickName
 		 */
		if (SensitiveWordsKit.checkSensitiveWord(c.getPara("user.nickName")) != null) {
			addError("msg", "昵称不能包含敏感词");
		}
		validateRequired("user.nickName", "msg", "昵称不能为空");
		validateString("user.nickName", 1, 19, "msg", "昵称不能超过19个字");

		String nickName = c.getPara("user.nickName").trim();
		// 全角半角都要判断
		if (nickName.contains("@") || nickName.contains("＠")) {
			addError("msg", "昵称不能包含 \"@\" 字符");
		}
		if (nickName.contains(" ") || nickName.contains("　")) {
			addError("msg", "昵称不能包含空格");
		}

		/**
		 * 验证 userName
		 */
		validateRequired("user.email", "msg", "邮箱不能为空");
		validateEmail("user.email", "msg", "邮箱格式不正确");
	}

	@Override
	protected void handleError(Controller c) {
		c.setAttr("state", "fail");
		c.renderJson();
	}
}